import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy, globalConfig, $http } = window
const { COVIDUrl } = globalConfig
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $time 	= $Lazy.file(()=>import('@com/utils/time'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
const statusData = [{name: '正常', value: 1}, {name: '作废', value: 2}]
const isMultipleData = [{name: '单采', value: 0}, {name: '混采', value: 1}]
const checkTypeData = [{name: '人员', value: 0}, {name: '物表', value: 1}]
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '账期编号',			name: 'summary_sn',		type:'date-time',	format: 5,	picker: 'month',	onValue:({value})=>{
				if(value) this.model.summary_sn = value.replace('-','')
			} },
			{ label: '条码号',			name: 'spec_code' },
			{ label: '业务区域',			name: 'area_id',  				 type: 'select', 		loading:true },
			{ label: '创建日期',			names: ['created_at_start','created_at_end'], name:'jz-time', type:'date-range' },
			{ label: '接收日期',			names: ['check_time_start','check_time_end'], name:'checkTime-time', type:'date-range' },
			{ label: '送检单位',			name: 'hosp_id',			 	type: 'select', 		loading:true,	idStr:'uuid',	nameStr:'hospName' },
			{ label: '业务员',			name: 'salesman_id', 			type: 'select', 		loading:true,		onValue:({value})=>{
				if (value) {
					const v = this.salesmanData?.find(v => v.value === value)
					this.model.salesman_id = v.number
				}
			} },
			{ label: '身份证',			name: 'card_no' },
			{ label: '账单状态',			name: 'status',					type: 'select',			data: statusData },
			{ label: '采集类型',			name: 'is_multiple',				type: 'select',			data: isMultipleData },
			{ label: '检测类型',			name: 'check_type',				type: 'select',			data: checkTypeData },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
	}
	onTime = () => {
		$time.then(f=>{
			const arr = f.duration['1']()
			this.resetModel = { created_at_start: arr[0], created_at_end: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.fetch()
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 区域
			this.areaData = await f.getCache({ api:'bs_area' } )
			$fn.setRow(forms, 'area_id', this.areaData ) 
			// 医院名称
			this.hospData = await f.getCache({ api:COVIDUrl+'Hospital/query',param:{page:1,page_num:10000,status:1} } )
			$fn.setRow(forms, 'hosp_id', this.hospData )
			// 业务员
			this.salesmanData = await f.getCache({ api:'salesman' } )
			$fn.setRow(forms, 'salesman_id', this.salesmanData )
			this.setState({ forms })
			this.onTime()
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,COVIDUrl+'fibill/query',{COVID:true,isIndex:true})
	
	// 表单
	cols = [
		{ title: '账期编号',				dataIndex: 'summarySn',				width:100 },
		{ title: '条码号',				dataIndex: 'specCode',				...$fn.table.spec_code },
		{ title: '医院名称',				dataIndex: 'hospId',				width:150,				render: text => $fn.getState(this.hospData,text,'hospName','uuid') },
		{ title: '业务区域',				dataIndex: 'areaId',				width:150,				render: text => $fn.getState(this.areaData,text) },
		{ title: '姓名',					dataIndex: 'patientName',			...$fn.table.name },
		{ title: '业务员',				dataIndex: 'salesmanUser',			...$fn.table.name },
		{ title: '身份证',				dataIndex: 'cardNo',				...$fn.table.idcard },
		{ title: '电话',					dataIndex: 'patientPhone',			...$fn.table.phone },
		{ title: '科室',					dataIndex: 'departmentName',		width:180 },
		{ title: '采集类型',				dataIndex: 'isMultiple',			...$fn.table.name,		render: text => $fn.getState(isMultipleData,text) },
		{ title: '检测类型',				dataIndex: 'checkType',				...$fn.table.name,		render: text => $fn.getState(checkTypeData,text) },
		{ title: '接收时间',				dataIndex: 'checkTime',				...$fn.table.time },
		{ title: '账单状态',				dataIndex: 'status',				...$fn.table.num,		render: text => $fn.getState(statusData,text) },
		{ title: '账单创建日期', 			dataIndex: 'createdAt', 			...$fn.table.time },
		{ title: '标准价格',				dataIndex: 'sprice',				...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'price',					...$fn.table.num },
		{ title: '备注',					dataIndex: 'remark',				width:150 },
	]
	
	controls = () => {
		return [
			Controls.click.call(this,'导出', ()=>{
				$http.pull(null, COVIDUrl + 'fibill/query', {param: {...this.model, download:1}}).then((data)=>{
					if(data){
						window.open(`${COVIDUrl}download?rand=${data}`)
					}
				})
			},{ghost:true,code:'F1'})
		]
	}
	
	render(){
		return (
			<>
				<Page title='财务管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1' }) }
					{/* 表格 */}
					{ Table.call(this,{isIndex:true}) }
				</Page>
			</>
		)
	}
}

export default Index